package com.rzj.book.dao;

import com.rzj.book.en.Emp;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @ClassName IEmpDao
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/17 13:52
 * @Version 1.0
 */
public interface IEmpDao extends Mapper<Emp>{

    @Select("select * from emp where username = #{username} and pwd = #{pwd}")
    Emp findByUsernameAndPwd(@Param("username")String username,@Param("pwd") String pwd);

    @Select("select * from emp where empId = #{empId}")
    Emp findEmpById(Long empId);

    @Select(" select e.* from emp e LEFT JOIN emp_role er on er.empId = e.empId " +
            " left join role r on r.roleId = er.roleId " +
            " where r.roleId = #{roleId}")
    List<Emp> getEmpListByRoleId(Integer roleId);

    @Select("select * from emp")
    List<Emp> getEmpList();

    @Select("select * from emp where empId = #{id}")
    @Results({
            @Result(id=true,column="empId",property="empId"),
            @Result(column="username",property="username"),
            @Result(column="gender",property="gender"),
            @Result(column="email",property="email"),
            @Result(column="tele",property="tele"),
            @Result(column="address",property="address"),
            @Result(column="birthday",property="birthday"),
            @Result(column="empId",property="roles",many=@Many(select="com.rzj.book.dao.IRoleDao.getRoleListByEmpId",fetchType = FetchType.LAZY))
    })
    Emp getEmpByEmpId(Integer id);

    @Delete("delete from emp where empId = #{id}")
    void deleteEmpById(Integer id);
}
